package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.quality.domain.req.BcStationHqDetailReq;
import com.ruibang.glass.quality.domain.req.QualityThicknessReq;
import com.ruibang.glass.quality.domain.req.SemiCheckReportReq;
import com.ruibang.glass.quality.domain.resp.DecideResultResp;
import com.ruibang.glass.quality.domain.resp.QualityGlassResp;
import com.ruibang.glass.quality.domain.resp.SemiCheckDetailData;
import com.ruibang.glass.quality.domain.resp.SemiCheckReportResp;
import com.ruibang.glass.quality.entity.BcStationHqDetail;
import com.ruibang.glass.quality.entity.QualityThickness;
import com.ruibang.glass.quality.mapper.BcStationHqDetailMapper;
import com.ruibang.glass.quality.service.*;
import com.teaming.cloud.framework2.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2023-11-15  09:17
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class BcStationHqDetailServiceImpl extends ServiceImpl<BcStationHqDetailMapper, BcStationHqDetail> implements BcStationHqDetailService {

    @Autowired
    private QualityThicknessService thicknessService;

    @Autowired
    private QualityWarpingService warpingService;

    @Autowired
    private QualityStressService stressService;

    @Autowired
    private QualityStripeService stripeService;

    @Autowired
    private QualityRippleService rippleService;

    @Autowired
    private QualityDeflectService deflectService;

    @Autowired
    private QualityGlassSizeService glassSizeService;

    @Override
    public List<BcStationHqDetail> queryDataByTime(BcStationHqDetailReq bcStationHqDetailReq) {
        return baseMapper.queryDataByTime(bcStationHqDetailReq);
    }

    @Override
    public List<QualityGlassResp> queryGlassIdByParam(String glassId) {
        return baseMapper.queryGlassIdByParam(glassId);
    }

    @Override
    public SemiCheckReportResp queryCheckReportData(SemiCheckReportReq reportReq) {
        SemiCheckReportResp resp = new SemiCheckReportResp();
        if(reportReq == null ) {
            reportReq = new SemiCheckReportReq();
            reportReq.setTransectStartTime(LocalDateTime.now());
        }else if (reportReq.getTransectStartTime()==null){
            reportReq.setTransectStartTime(LocalDateTime.now());
        }
        LocalDateTime endTime = reportReq.getTransectStartTime().plusDays(1);
        reportReq.setTransectEndTime(endTime);

        Map<String, SemiCheckDetailData> checkDataMap = new HashMap<>();
        //厚度数据
        QualityThicknessReq thicknessReq = new QualityThicknessReq();
        thicknessReq.setTransectStartTime(reportReq.getTransectStartTime());
        thicknessReq.setTransectEndTime(endTime);
        List<QualityThickness> thicknesses = thicknessService.queryQualityThickness(thicknessReq);
        if(CollectionUtils.isNotEmpty(thicknesses)){
            float max = 0.0f;
            float min = 1000f;
            float total = 0.0f;
            float noFlowMax = 0.0f;
            float fullMax = 0.0f;
            for (QualityThickness tn : thicknesses) {
                if(tn.getFullThicknessMax()!=null&&tn.getFullThicknessMax()>max){
                    max = tn.getFullThicknessMax();
                }
                if(tn.getFullThicknessMin()!=null&&tn.getFullThicknessMin()<min){
                    min = tn.getFullThicknessMin();
                }
                if(tn.getFullThicknessAverage()!=null){
                    total = total + tn.getFullThicknessAverage();
                }
                if(tn.getChangeNoFlow()!=null&&tn.getChangeNoFlow()>noFlowMax){
                    noFlowMax = tn.getChangeNoFlow();
                }
                if(tn.getChangeFull()!=null&&tn.getChangeFull()>fullMax){
                    fullMax = tn.getChangeFull();
                }
                if(StringUtils.isNotBlank(tn.getGlassId())){
                    SemiCheckDetailData detail = checkDataMap.get(tn.getGlassId());
                    if(detail == null){
                        detail = new SemiCheckDetailData();
                    }
                    if(detail.getTransectTime()==null&&tn.getTransectTime()!=null){
                        detail.setTransectTime(tn.getTransectTime());
                        detail.setCheckTime(tn.getTransectTime().getHour()+":"+tn.getTransectTime().getMinute());
                    }

                    if(StringUtils.isNotBlank(tn.getDecide())){
                        detail.setThickness(tn.getDecide());
                    }
                    detail.setGlassId(tn.getGlassId());
                    detail.setThicknessMax(tn.getFullThicknessMax());
                    detail.setThicknessMin(tn.getFullThicknessMin());
                    detail.setThicknessAverage(tn.getFullThicknessAverage());
                    detail.setChangeFull(tn.getChangeFull());
                    detail.setChangeFlow(tn.getChangeFlow());
                    detail.setChangeNoFlow(tn.getChangeNoFlow());
                    checkDataMap.put(tn.getGlassId(),detail);
                }
            }

            float average = total/thicknesses.size();
            resp.setThicknessMax(max);
            if(min==1000){
                resp.setThicknessMin(0.0f);
            }else{
                resp.setThicknessMin(min);
            }
            resp.setThicknessAverage(average);
            resp.setChangeNoFlowMax(noFlowMax);
            resp.setChangeFullMax(fullMax);
        }

        //翘曲
        List<SemiCheckDetailData> warpingCheckData = warpingService.queryWarpingCheckData(reportReq);
        if(CollectionUtils.isNotEmpty(warpingCheckData)){
            float max = 0.0f;
            for (SemiCheckDetailData wcd : warpingCheckData) {
                if(wcd.getASideWarping()!=null&&wcd.getASideWarping()>max){
                    max = wcd.getASideWarping();
                }
                if(wcd.getBSideWarping()!=null&&wcd.getBSideWarping()>max){
                    max = wcd.getBSideWarping();
                }
                if(StringUtils.isNotBlank(wcd.getGlassId())){
                    SemiCheckDetailData detail = checkDataMap.get(wcd.getGlassId());
                    if(detail == null){
                        detail = new SemiCheckDetailData();
                    }
                    if(StringUtils.isNotBlank(wcd.getWarping())){
                        detail.setWarping(wcd.getWarping());
                    }
                    if(detail.getTransectTime()==null&&wcd.getTransectTime()!=null){
                        detail.setTransectTime(wcd.getTransectTime());
                        detail.setCheckTime(wcd.getTransectTime().getHour()+":"+wcd.getTransectTime().getMinute());
                    }
                    detail.setGlassId(wcd.getGlassId());
                    detail.setASideWarping(wcd.getASideWarping());
                    detail.setBSideWarping(wcd.getBSideWarping());

                    checkDataMap.put(wcd.getGlassId(),detail);
                }
            }

            resp.setWarpingMax(max);
        }
        //应力
        List<SemiCheckDetailData> stressCheckData = stressService.queryStressCheckData(reportReq);
        if(CollectionUtils.isNotEmpty(stressCheckData)){
            float max = 0.0f;
            for (SemiCheckDetailData scd : stressCheckData) {
                if(scd.getStressMax()!=null&&scd.getStressMax()>max){
                    max = scd.getStressMax();
                }
                if(StringUtils.isNotBlank(scd.getGlassId())){
                    SemiCheckDetailData detail = checkDataMap.get(scd.getGlassId());
                    if(detail == null){
                        detail = new SemiCheckDetailData();
                    }

                    if(StringUtils.isNotBlank(scd.getStress())){
                        detail.setStress(scd.getStress());
                    }

                    if(detail.getTransectTime()==null&&scd.getTransectTime()!=null){
                        detail.setTransectTime(scd.getTransectTime());
                        detail.setCheckTime(scd.getTransectTime().getHour()+":"+scd.getTransectTime().getMinute());
                    }
                    detail.setGlassId(scd.getGlassId());
                    detail.setStressMax(scd.getStressMax());
                    detail.setStressMin(scd.getStressMin());
                    detail.setStressAverage(scd.getStressAverage());
                    checkDataMap.put(scd.getGlassId(),detail);
                }
            }
            resp.setStressMax(max);
        }

        //条纹
        List<SemiCheckDetailData> stripeCheckData = stripeService.queryStripeCheckData(reportReq);
        if(CollectionUtils.isNotEmpty(stripeCheckData)){
            float stripePercent = 0.0f;
            float lightShadeLinePercent = 0.0f;
            for (SemiCheckDetailData scd : stripeCheckData) {
                if(CommonConstant.ZERO_STR.equals(scd.getStripe())){
                    stripePercent = stripePercent+1;
                }
                if(CommonConstant.ZERO_STR.equals(scd.getLightShadeLine())){
                    lightShadeLinePercent = lightShadeLinePercent+1;
                }
                if(StringUtils.isNotBlank(scd.getGlassId())){
                    SemiCheckDetailData detail = checkDataMap.get(scd.getGlassId());
                    if(detail == null){
                        detail = new SemiCheckDetailData();
                    }

                    if(StringUtils.isNotBlank(scd.getStripe())){
                        detail.setStripe(scd.getStripe());
                    }

                    if(StringUtils.isNotBlank(scd.getLightShadeLine())){
                        detail.setLightShadeLine(scd.getLightShadeLine());
                    }

                    if(detail.getTransectTime()==null&&scd.getTransectTime()!=null){
                        detail.setTransectTime(scd.getTransectTime());
                        detail.setCheckTime(scd.getTransectTime().getHour()+":"+scd.getTransectTime().getMinute());
                    }
                    detail.setGlassId(scd.getGlassId());
                    checkDataMap.put(scd.getGlassId(),detail);
                }
            }
            int size = stripeCheckData.size();
            resp.setStripePercent(stripePercent/size * 100);
            resp.setLightShadeLinePercent(lightShadeLinePercent/size * 100);
        }

        //波纹度
        List<SemiCheckDetailData> rippleCheckData = rippleService.queryRippleCheckData(reportReq);
        if(CollectionUtils.isNotEmpty(rippleCheckData)){
            float max = 0.0f;
            for (SemiCheckDetailData rcd : rippleCheckData) {
                if(rcd.getRippleDegree()!=null&&rcd.getRippleDegree()>max){
                    max = rcd.getRippleDegree();
                }
                if(StringUtils.isNotBlank(rcd.getGlassId())){
                    SemiCheckDetailData detail = checkDataMap.get(rcd.getGlassId());
                    if(detail == null){
                        detail = new SemiCheckDetailData();
                    }
                    if(StringUtils.isNotBlank(rcd.getRipple())){
                        detail.setRipple(rcd.getRipple());
                    }
                    if(detail.getTransectTime()==null&&rcd.getTransectTime()!=null){
                        detail.setTransectTime(rcd.getTransectTime());
                        detail.setCheckTime(rcd.getTransectTime().getHour()+":"+rcd.getTransectTime().getMinute());
                    }
                    detail.setGlassId(rcd.getGlassId());
                    checkDataMap.put(rcd.getGlassId(),detail);
                }
            }

            if(max>0.0){
                resp.setRippleMax(max);
            }
        }
        //挠度
        List<SemiCheckDetailData> deflectCheckData = deflectService.queryDeflectCheckData(reportReq);
        if(CollectionUtils.isNotEmpty(deflectCheckData)){
            float max = 0.0f;
            for (SemiCheckDetailData dcd : deflectCheckData) {
                if(dcd.getDeflectSpecification()!=null&&dcd.getDeflectSpecification()>max){
                    max = dcd.getDeflectSpecification();
                }
                if(StringUtils.isNotBlank(dcd.getGlassId())){
                    SemiCheckDetailData detail = checkDataMap.get(dcd.getGlassId());
                    if(detail == null){
                        detail = new SemiCheckDetailData();
                    }
                    if(StringUtils.isNotBlank(dcd.getDeflect())){
                        detail.setDeflect(dcd.getDeflect());
                    }
                    if(detail.getTransectTime()==null&&dcd.getTransectTime()!=null){
                        detail.setTransectTime(dcd.getTransectTime());
                        detail.setCheckTime(dcd.getTransectTime().getHour()+":"+dcd.getTransectTime().getMinute());
                    }
                    detail.setGlassId(dcd.getGlassId());
                    checkDataMap.put(dcd.getGlassId(),detail);
                }
            }

            if(max>0){
                resp.setDeflectMax(max);
            }
        }
        //尺寸
        List<SemiCheckDetailData> glassSizeCheckData = glassSizeService.queryGlassSizeCheckData(reportReq);
        if(CollectionUtils.isNotEmpty(glassSizeCheckData)){
            for (SemiCheckDetailData gscd : glassSizeCheckData) {
                if(StringUtils.isNotBlank(gscd.getGlassId())){
                    SemiCheckDetailData detail = checkDataMap.get(gscd.getGlassId());
                    if(detail == null){
                        detail = new SemiCheckDetailData();
                    }
                    if(StringUtils.isNotBlank(gscd.getGlassSize())){
                        detail.setGlassSize(gscd.getGlassSize());
                    }
                    if(detail.getTransectTime()==null&&gscd.getTransectTime()!=null){
                        detail.setTransectTime(gscd.getTransectTime());
                        detail.setCheckTime(gscd.getTransectTime().getHour()+":"+gscd.getTransectTime().getMinute());
                    }
                    detail.setGlassId(gscd.getGlassId());
                    checkDataMap.put(gscd.getGlassId(),detail);
                }
            }
        }

        if(!checkDataMap.isEmpty()){
            List<SemiCheckDetailData> datas = new ArrayList<>();
            for (SemiCheckDetailData data : checkDataMap.values()) {
                if(CommonConstant.ONE_STR.equals(data.getThickness())
                        ||CommonConstant.ONE_STR.equals(data.getWarping())
                        ||CommonConstant.ONE_STR.equals(data.getStress())
                        ||CommonConstant.ONE_STR.equals(data.getStripe())
                        ||CommonConstant.ONE_STR.equals(data.getLightShadeLine())
                        ||CommonConstant.ONE_STR.equals(data.getRipple())
                        ||CommonConstant.ONE_STR.equals(data.getDeflect())
                        ||CommonConstant.ONE_STR.equals(data.getGlassSize())
                ){
                    data.setOverallDecide(CommonConstant.ONE_STR);
                }
                datas.add(data);
            }
            datas = datas.stream().sorted(Comparator.comparing(SemiCheckDetailData::getTransectTime)).collect(Collectors.toList());
            resp.setDetailData(datas);
        }
        return resp;
    }

    @Override
    public List<DecideResultResp> queryDecideResult(List<String> glassCodes) {
        List<DecideResultResp> resps = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(glassCodes)){
            resps = baseMapper.queryDecideResult(glassCodes);
        }
        return resps;
    }


}
